package cn.hrlly.tao2shou.callback;

import com.google.gson.Gson;
import com.zhy.http.okhttp.callback.Callback;

import cn.hrlly.tao2shou.R;
import cn.hrlly.tao2shou.domain.Commodity;
import cn.hrlly.tao2shou.domain.Require;
import cn.hrlly.tao2shou.listener.OnRequireDetailsListener;
import okhttp3.Request;
import okhttp3.Response;

/**
 * User: gengliang
 * Date: 2016-01-30
 * Time: 10:31
 */

public class RequireDetailsCallback extends Callback<Require> {

    private OnRequireDetailsListener onRequireDetailsListener;

    public RequireDetailsCallback(OnRequireDetailsListener onRequireDetailsListener){
        this.onRequireDetailsListener=onRequireDetailsListener;
    }

    @Override
    public Require parseNetworkResponse(Response response) throws Exception {
        String string=response.body().string();
        Require require=new Gson().fromJson(string,Require.class);
        return require;
    }

    @Override
    public void onError(Request request, Exception e) {
        onRequireDetailsListener.showRequireDetailsFail(R.string.network_connection_error);
    }

    @Override
    public void onResponse(Require require) {
        if(require.getStatus()==0)
            onRequireDetailsListener.showRequireDetailsSucc(require);
        else
            onRequireDetailsListener.showRequireDetailsFail(require.getErrorMessage());
    }
}
